package com.example.micrauth.contorller;

import com.example.common.constanst.SysConstant;
import com.example.micrauth.domain.dto.LoginDTO;
import com.example.micrauth.service.AuthenticationService;
import com.example.micrauth.domain.vo.RespResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Tag(name = "权限认证")
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthController {

    @Autowired
    private AuthenticationService authenticationService;


    @Operation(summary = "登录接口", description = "根据用户的账号密码和token进行身份验证")
    @PostMapping("/login")
    public RespResult sysLogin(@RequestBody LoginDTO loginDTO, @Nullable @RequestHeader("Authorization") String token) {
        log.info("controller get loginDTO = {}", loginDTO);
//        log.info("Authentication = {}", SecurityContextHolder.getContext().getAuthentication());
        Map<String, Object> map = authenticationService.login(loginDTO, token);
        log.info("controller map = {}", map);
        return RespResult.success().Data(map);
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public RespResult sysLogout(HttpServletRequest request) {
        String token = request.getHeader(SysConstant.AUTHORIZATION);
        RespResult respResult = authenticationService.logout(token);
        return respResult;
    }
}
